﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Net;
using System.Net.NetworkInformation;

namespace WindowsFormsApplication1
{
    class InterfaceSensor : Sensor
    {
        private List<string> interfaces;
        private Timer myTimer;
        private const int TIMER_INTERVAL = 20000;
        public InterfaceSensor()
        {
            interfaces = new List<string>();
            name = "Interface Sensor";
            myTimer = new Timer(); //Initialize Timer
            myTimer.Elapsed += new ElapsedEventHandler(InterfaceEventArrived);
            myTimer.Interval = TIMER_INTERVAL;
            myTimer.Start();
            populate();
            data = interfaces;
            Notify();
        }
        private void populate()
        {
            interfaces.Clear();
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                NetworkInterface[] i = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in i)
                {
                    string x="Interface Name : ";
                    x = x + ni.Name.ToString();
                    //x = x + ni.Id.ToString()+" / ";
                    x = x + ni.Id.ToString();
                    interfaces.Add(x);
                    //Console.WriteLine("Interface Name : {0}", ni.Name.ToString());
                    //Console.WriteLine(ni.Id);
                }
            }

        }
        public void InterfaceEventArrived(object source, ElapsedEventArgs f)
        {
            populate();
            data = interfaces;
            Notify();
        }
    }
}
